package com.powernode.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powernode.domain.Clue;
import com.powernode.domain.Customer;
import com.powernode.listener.DemoDataListener;
import com.powernode.mapper.ClueMapper;
import com.powernode.mapper.CustomerMapper;
import com.powernode.model.BaseQuery;
import com.powernode.service.ClueService;
import com.powernode.util.AuthUtils;
import com.powernode.vo.ClueVo;
import com.powernode.vo.PieData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageInfo<ClueVo> queryClueListByPage(Integer current, Integer size) {
        PageHelper.startPage(current,size);
        List<ClueVo> list = clueMapper.selectList(new BaseQuery());
        return new PageInfo<>(list);
    }

    @Override
    public Boolean saveClue(Clue clue) {
        clue.setOwnerId(AuthUtils.getLoginUserId());
        clue.setCreateBy(AuthUtils.getLoginUserId());
        clue.setCreateTime(new Date());
        return clueMapper.insertSelective(clue)>0;
    }

    @Override
    public Clue queryClueInfoById(Integer clueId) {
        return clueMapper.selectByPrimaryKey(Long.valueOf(clueId));
    }

    @Override
    public Boolean modifyClue(Clue clue) {
        clue.setEditBy(AuthUtils.getLoginUserId());
        clue.setEditTime(new Date());
        return clueMapper.updateByPrimaryKeySelective(clue)>0;
    }

    @Override
    public void batchSaveClue(MultipartFile file) throws IOException {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(file.getInputStream(), Clue.class, new DemoDataListener(clueMapper)).sheet().doRead();
    }

    @Override
    public Boolean removeClueById(Integer clueId) {

        return clueMapper.deleteByPrimaryKey(Long.valueOf(clueId))>0;
    }

    @Override
    public Boolean removeClueByIds(List<Integer> ids) {
        return clueMapper.batchDeleteClueByIds(ids)==ids.size();
    }

    @Override
    public ClueVo queryClueDetailInfoById(Integer id) {
        return clueMapper.selectClueDetailInfoById(id);
    }

    @Override
    public List<PieData> queryNotewayList() {
        return clueMapper.selectNotewayList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean transferToCustomer(Integer clueId) {
        //修改线索状态位"已转换客户"
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setState(-1);
        clue.setEditTime(new Date());
        clue.setEditBy(AuthUtils.getLoginUserId());
        int i = clueMapper.updateByPrimaryKeySelective(clue);
        //新增客户记录
        if (i>0){
            Clue clueInfo = clueMapper.selectByPrimaryKey(Long.valueOf(clueId));
            Customer customer = new Customer();
            customer.setClueId(clueId);
            customer.setCreateBy(AuthUtils.getLoginUserId());
            customer.setCreateTime(new Date());
            customer.setProduct(clueInfo.getIntentionProduct());
            customer.setDescription(clueInfo.getDescription());
            customerMapper.insertSelective(customer);
        }
        return i>0;
    }
}
